//
//  CardCirleViewController.m
//  PaiQuan
//
//  Created by Mac on 16/7/5.
//  Copyright © 2016年 huigu. All rights reserved.
//

#import "CardCirleViewController.h"
#import "ListConversationController.h"
#import "ConversationViewController.h"
#import "ClubViewController.h"
#import "addView.h"
#import "AddFriendController.h"
#import "addClubController.h"
#import "bulidCulbController.h"

#define scrollViewH ScreenH-self.titleView.frame.size.height-self.friendBtn.frame.size.height-self.lineView.frame.size.height
@interface CardCirleViewController ()<UIScrollViewDelegate,selectIndexPathDelegate>
@property(nonatomic,strong)UIButton *friendBtn;
@property(nonatomic,strong)UIButton *clubBtn;
@property(nonatomic,strong)UIView *lineView;
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,assign)NSInteger pageIndex;
@property(nonatomic,strong)UIButton *oldBtn;
@property(nonatomic,strong)UIView *coverView;
@end

@implementation CardCirleViewController

- (void)viewDidLoad {
  [super viewDidLoad];
    //第一次登入默认选定我的好友
    self.friendBtn.selected = YES;
    [self.friendBtn setTitleColor:PQ_COLOR_YELLOW forState:UIControlStateSelected];
    self.friendBtn.userInteractionEnabled = NO;
    [self setUPNav];
    [self setUpUI];
    [self setUpConstraints];
    //scrollView设定
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    
   
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.bounces = NO;
    self.scrollView.contentSize = CGSizeMake(ScreenW*2, 0);
    self.pageControl.numberOfPages =2;
    self.pageControl.currentPage = 0;
    for ( NSInteger i = 0; i<2; i++) {
        if (i == 0) {
           ListConversationController  *ListVC = [[ListConversationController alloc] init];
            ListVC.view.frame = CGRectMake(0, 0, ScreenW, scrollViewH);
            ListVC.view.backgroundColor = [UIColor redColor];
            [self.scrollView  addSubview:ListVC.view];
            [self addChildViewController:ListVC];
            
        }else{
          ClubViewController  *ConversationVC = [[ClubViewController alloc] init];
            
            ConversationVC.view.frame = CGRectMake(ScreenW, 0, ScreenW, scrollViewH);
          
             [self.scrollView  addSubview:ConversationVC.view];
            [self addChildViewController:ConversationVC];
        }
        
        
    }
    

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 在滚动的时候调用
    // 我么发现 offset 在 除以 width 的时候, 的结果 恰好是 页码数
    
    NSInteger pageIndex = (scrollView.contentOffset.x + scrollView.bounds.size.width / 2) / scrollView.bounds.size.width;
    
    
    // NSLog(@"%ld", pageIndex);
    
    self.pageControl.currentPage = pageIndex;
    if (pageIndex == 0) {
        self.friendBtn.selected = YES;
        self.friendBtn.userInteractionEnabled = NO;
        [ self.friendBtn setTitleColor:PQ_COLOR_YELLOW forState:UIControlStateSelected];

        self.clubBtn.selected = NO;
        self.clubBtn.userInteractionEnabled = YES;
        [UIView animateWithDuration:0.1 animations:^{
            self.lineView.transform = CGAffineTransformMakeTranslation(0,0);
        }];

    }else{
        self.clubBtn.selected = YES;
        self.clubBtn.userInteractionEnabled = NO;

        self.friendBtn.selected = NO;
        self.friendBtn.userInteractionEnabled = YES;
        [self.clubBtn setTitleColor:PQ_COLOR_YELLOW forState:UIControlStateSelected];

        [UIView animateWithDuration:0.1 animations:^{
            self.lineView.transform = CGAffineTransformMakeTranslation(RATIO(382.5),0);
        }];
	
        
    }
    
    
    
}
-(void)setUpUI{
    [self.view addSubview:self.friendBtn];
    [self.view addSubview:self.clubBtn];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.coverView];
    
}
-(void)setUpConstraints{
    [self.friendBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleView.bottom);
        make.left.equalTo(self.view);
        make.height.equalTo(RATIO(68));
        make.width.equalTo(ScreenW/2);
    }];
    [self.clubBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleView.bottom);
        make.right.equalTo(self.view.right);
        make.height.equalTo(RATIO(68));
        make.width.equalTo(ScreenW/2);
    }];
    [self.lineView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.friendBtn.bottom);
        make.width.equalTo(RATIO(200));
        make.centerX.equalTo(self.friendBtn);
        make.height.equalTo(RATIO(4));
    }];
    [self.scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView.bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
   
    
}

-(void)setUPNav{
  
    self.titleView.title = @"牌圈";
    self.titleView.backgroundColor = PQ_COLOR_BLACK;
    [self.titleView.btnBack setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [self.titleView.btnBack addTarget:self action:@selector(addBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    self.titleView.isHidebtnBack = NO;
    [self.titleView.rightBtn setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    
    
    
}
#pragma mark --按钮的点击事件
-(void)addBtnDidClick:(UIButton*)sender{

    CGPoint point = CGPointMake(sender.frame.origin.x+sender.frame.size.width*0.7,sender.frame.origin.y+sender.frame.size.height-RATIO(20));
    addView *view1 = [[addView alloc] initWithOrigin:point Width:RATIO(250) Height:RATIO(180) Type: XTTypeOfUpLeft Color:PQ_COLOR_MIDGray];
    view1.dataArray = @[@"添加朋友",@"加入俱乐部", @"新建俱乐部"];
    view1.images = @[@"添加朋友", @"加入俱乐部", @"新建俱乐部"];
    view1.fontSize = RATIO(28);
    view1.row_height = RATIO(60);
    view1.titleTextColor = [UIColor whiteColor];
    view1.delegate = self;
    [view1 popView];
    
    
}

#pragma mark -addview里面的点击跳转方法
- (void)selectIndexPathRow:(NSInteger)index coverView:(UIView *)coverView addView:(UIView *)addView
{
    [addView removeFromSuperview];
    [coverView removeFromSuperview];
 
    
    switch (index) {
        case 0:
        {
        
          
            AddFriendController *addFriendVc = [[AddFriendController alloc] init];
            
            [self.navigationController pushViewController:addFriendVc animated:YES];
            
        }
            break;
        case 1:
        {
            addClubController *addClubVC = [[addClubController alloc] init];
             [self.navigationController pushViewController:addClubVC animated:YES];
            
        }
            break;
        case 2:
        {
            bulidCulbController *buildVC = [[bulidCulbController alloc] init];
            [self.navigationController pushViewController:buildVC animated:YES];
        
        }
            break;
        case 3:
        {
            NSLog(@"Clikc 3 ......");
        }
            break;
        default:
            break;
    }
}
/**
 *  　懒加载
 */
-(UIButton*)clubBtn{
    if (!_clubBtn) {
        _clubBtn = [[UIButton alloc] init];
        [_clubBtn setTitle:@"俱乐部" forState:UIControlStateNormal];
        [_clubBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _clubBtn.backgroundColor = PQ_COLOR_BLACK;
       [_clubBtn.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
        _clubBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
       [_clubBtn addTarget:self action:@selector(BtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        _clubBtn.tag = 1;
    }
    
    return _clubBtn;
    
    
}
-(UIButton*)friendBtn{
    if (!_friendBtn) {
        _friendBtn = [[UIButton alloc] init];
        _friendBtn.backgroundColor = PQ_COLOR_BLACK;
        [_friendBtn setTitle:@"好友" forState:UIControlStateNormal];
        [_friendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [_friendBtn addTarget:self action:@selector(BtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_friendBtn.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
         _friendBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        _friendBtn.tag = 0;
    }
    
    return _friendBtn;
    
    
}
-(UIView*)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = PQ_COLOR_YELLOW;
    }
    return _lineView;
    
    
    
}
#pragma mark --按钮的点击时间
-(void)BtnDidClick:(UIButton*)sender{
    //按钮状态的设置
    
    sender.selected = YES;
    sender.userInteractionEnabled = NO;
    [sender setTitleColor:PQ_COLOR_YELLOW forState:UIControlStateSelected];

        if (sender.tag==0) {
            self.clubBtn.selected = NO;
            self.clubBtn.userInteractionEnabled = YES;
        [UIView animateWithDuration:0.1 animations:^{
            self.lineView.transform = CGAffineTransformMakeTranslation(0,0);
        }];
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES ];
    }else{
        self.friendBtn.selected = NO;
        self.friendBtn.userInteractionEnabled = YES;
        [UIView animateWithDuration:0.1 animations:^{
            self.lineView.transform = CGAffineTransformMakeTranslation(RATIO(382.5),0);
        }];
          [self.scrollView setContentOffset:CGPointMake(ScreenW,0 ) animated:YES];

    }
    

//    
//
    
        
   
        //线的设置
  
    
    
}

-(UIScrollView*)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        
    }
    return _scrollView;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
